﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WisDom.IService;
using WisDom.Util;

namespace WisDom.Update.Controllers
{
    /// <summary>
    /// 包文件管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PackageFileController : ControllerBase
    {
        IPackageSetupInfoInterface packagesetupinfoService;
        public PackageFileController(IPackageSetupInfoInterface packagesetupinfoService)
        {
            this.packagesetupinfoService = packagesetupinfoService;
        }
        /// <summary>
        /// 获取下载的文件路径
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}", Name = "GetDownloadPath")]
        public async Task<IActionResult> GetDownloadFilePath(int id)
        {
            string filePath = "";
            var entity = await packagesetupinfoService.GetEntity(id.ToString());
            if (entity != null)
            {
                filePath = entity.SavePath;
            }
            return new JsonResult(new { status = string.IsNullOrEmpty(filePath) ? false : true, webFilePath = filePath });
        }
        /// <summary>
        /// 获取最新的版本
        /// </summary>
        /// <param name="version"></param>
        /// <param name="packageType">包类型</param>
        /// <param name="hostFile">宿主文件</param>
        /// <returns></returns>
        [HttpGet(Name = "GetNewVersion")]
        public string GetNewVersion(string version, int packageId)
        {
            var entity = packagesetupinfoService.GetNewVersion(version, packageId);
            if (entity != null)
            {
                return entity.SavePath;
            }
            return "";
        }
    }
}